import $C  from '@/common/config.js';
import $S  from '@/store/index.js';
export default{
	 
	 common:{
		method:'GET', 
		header:{
			"content-type":"application/json"
		},
		data:{}
		 
	 },
	 
	 request(options={})
	 {
		 options.url = $C.webUrl+options.url
		 options.header = options.header || this.common.header 
		 options.method = options.method ||this.common.method
		 return new Promise((res,rej)=>{
			 uni.request({
				 ...options,
				 success:(result)=>{
					if(result.statusCode!==200){
						uni.showToast({
							title:result.data.msg || '请求失败',
							icon:'none'
						})
						 return rej(result.data);
					}
				   return res(result.data)
				 },
				 fail:(error)=>{
					 uni.showToast({
					 	title:error.errMsg || '请求失败',
					 	icon:'none'
					 });
					 return rej()
				 }
			 })
		 });
	},
	 get(url,data={},options={}){
		 options.url = url;
		 options.data = data;
		 options.method ="GET";
		 options.header = {
		 				 "content-type":"application/json",
		 				 "Authorization":uni.getStorageSync("token")
		 };
		 return  this.request(options)
	 },
	 post(url,data={},options={}){
	 		 options.url = url;
	 		 options.data = data;
			 options.header = {
				 "content-type":"application/json",
				 "Authorization":uni.getStorageSync("token")
			 };
	 		 options.method ="POST"
	 		 return this.request(options)
	 },
	 delete(url,data={},options={}){
	 		 options.url = url;
	 		 options.data = data;
	 			 options.header = {
	 				 "content-type":"application/json",
	 				 "Authorization":uni.getStorageSync("token")
	 			 };
	 		 options.method ="Delete"
	 		 return this.request(options)
	 },
	 
	 put(url,data={},options={}){
	 		 options.url = url;
	 		 options.data = data;
	 			 options.header = {
	 				 "content-type":"application/json",
	 				 "Authorization":uni.getStorageSync("token")
	 			 };
	 		 options.method ="PUT"
	 		 return this.request(options)
	 },
	 
	 postNoToken(url,data={},options={}){
	 		 options.url = url;
	 		 options.data = data;
	 		 options.method ="POST"
	 		 return this.request(options)
	 },
	 
	getNoToken(url,data={},options={}){
	 		 options.url = url;
	 		 options.data = data;
	 		 options.method ="GET"
	 		 return this.request(options)
	 }
	   
	   
	
}


